package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.entity.YddAdvpos;
import com.itheima.youdianda.service.YddAdvposService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ydd_advpos")
@Slf4j
public class YddAdvposController {

    @Autowired
    private YddAdvposService yddAdvposService;

    //分页查询
    @GetMapping("/list")
    public R<Page> list(String advposname, int page, int limit){
        log.info("advposname={},page={},limit={}",advposname,page,limit);
        //构造分页构造器
        Page pageInfo = new Page(page,limit);
        //构造条件构造器,添加过滤条件
        LambdaQueryWrapper<YddAdvpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(advposname),YddAdvpos::getAdvposname,advposname);
        yddAdvposService.page(pageInfo,queryWrapper);
        //执行查询
        return R.success(pageInfo);
    }

    //修改
    @PostMapping("/update")
    public R<String> update(@RequestBody YddAdvpos yddAdvpos){
        log.info("修改的员工信息:{}",yddAdvpos.toString());
        yddAdvposService.save(yddAdvpos);
        return R.success("修改成功!");
    }

    //删除
    @GetMapping("/del")
    public R<String> del(int id){
        log.info("删除广告的id为:{}",id);
        yddAdvposService.removeById(id);
        return R.success("删除信息成功!");
    }

    //广告位列表-全部
    @GetMapping("/all")
    public R<String> all(){
        return null;
    }

    //广告位详情
    @GetMapping("/rowInfo")
    public R<YddAdvpos> rowInfo(int id){
        YddAdvpos yddAdvpos = yddAdvposService.getById(id);
        return R.success(yddAdvpos);
    }

    //广告位添加
    @PostMapping("/add")
    public R<String> add(@RequestBody YddAdvpos yddAdvpos){
        log.info("新增广告信息:{}",yddAdvpos.toString());
        yddAdvposService.save(yddAdvpos);
        return R.success("新增信息成功!");
    }
}
